package jianzhiOffer;

import java.util.HashMap;
import java.util.Map;

public class Num011 {
    public int findMaxLength(int[] nums) {
        int n = nums.length,ans = 0;
        int[] sum = new int[n + 1];
        for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] +(nums[i - 1] == 0? -1 : 1);
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,0);
        for(int i = 1; i <= n; i++) {
            int t = sum[i];
            if(map.containsKey(t)) ans = Math.max(ans,i - map.get(t));
            else map.put(t,i);
        }
        return ans;
    }
}
